#include <stdio.h>

#define BUFSIZE	(1024*1024)

unsigned char  buffer[BUFSIZE];

void main (int argc, char **argv)
{
	FILE  *f, *dump;
	unsigned char  c;
	int  i, j, sz;

	if (argc < 3)
	{
		printf ("Usage: bin2c <output file> <filename> ...\n  Dumps the contents of files as C-source hex arrays to file 'output file'.\n");
		return;
	}
	
	dump = fopen (argv[1], "wb");
	if ( dump==NULL )
	{
		printf ("Can't create dump file.\n");
		return;
	}

	for (i=2; i < argc; ++i)
	{
		f = fopen (argv[i], "rb");
		if (f == NULL)
		{
			printf ("Can't open file '%s'.\n", argv[i]);
			continue;
		}
		if ( i>1 ) 
			fprintf (dump, "\n\n");
		for (j=0; (c=argv[i][j])!=0; ++j)
		{
			c = tolower(c);
			if (!isalnum(c))
				c = '_';
			argv[i][j] = c;
		}
		fprintf (dump, "unsigned char %s[] = {", argv[i]);
		do
		{
			sz = fread (buffer, 1, BUFSIZE, f);
			for (j=0; j<sz; ++j)
			{
				if (j%16==0)
					fprintf (dump, "\n ");
				fprintf (dump, " 0x%02X,", buffer[j]);
			}
		} while (sz==BUFSIZE);
		fprintf (dump, "\n};");
		fclose (f);
	}
	fclose (dump);
}
